'use strict';
(function(){
    var $html=$('html'),
        len,num,val,name,cid,i,j,
        $ajaxMessageBox=$('#ajaxMessageBox'),
        errorHtml = '<dl class="lg-web-error"><dt>Oops~网络错误</dt><dd>出现了网络错误，请检查您的网络是否正常⊙﹏⊙</dd></dl>',
        $parent,
        /* 首页轮播图 */
        $skSlider = $('#skSlider'),
        $checkboxScreen = $('.js_checkbox_screen'),
        $activeScreenSub = $('#activeScreenSub'),
        $activityMySchool = $('#activityMySchool');
    function moreRankAct(){//显示首页更多筛选项方法
        $html.off('click','.js_more_rank').on('click','.js_more_rank',function(){
            var $t = $(this);
            $t.toggleClass('active').parents('.js_rank_list').toggleClass('active');
        });
    };
    function moreRankDisplay(){//显示更多按钮显示、隐藏方法
        var $rankList = $('.js_rank_list'),
            obj,
            rankBtn,
            oWidth = 0,
            totalWidth = 0;
        len = $rankList.length;
        for(i=0;i<len;i++){
            obj = $rankList.eq(i).find('.js_screen_item');
            rankBtn = $rankList.eq(i).find('.js_more_rank');
            num = obj.length;
            totalWidth = 0;
            for(j=0;j<num;j++){
                oWidth = obj.eq(j).outerWidth(true);
                totalWidth = totalWidth + oWidth;
            }
            if(totalWidth > 580){
                rankBtn.show();
            }else{
                rankBtn.hide();
            }
        }
    };
    function timeScreenAct(){//按时间筛选方法
        var $timeScreenForm = $('#timeScreenForm'),
            startTime,
            strStartTime,
            endTime,
            strEndTime;
        seajs.use('dist/jquery.validate.min.js',function(){
            $timeScreenForm.validate({});
            $timeScreenForm.on('submit',function(){
                strStartTime = $timeScreenForm.find('input.js_startTime').val();
                strEndTime = $timeScreenForm.find('input.js_endTime').val();
                startTime = Date.parse(strStartTime);
                endTime = Date.parse(strEndTime);
                if(startTime > endTime){
                    ajax_message('结束时间不能早于开始时间',true);
                    setTimeout(function(){
                        $ajaxMessageBox.fadeOut();
                    },1000);
                    return false;
                }
            });
        });
    };
    function activeScreenAct(){//活动筛选方法
        $html.off('click','.js_screen_item').on('click','.js_screen_item',function(){
            var $t = $(this);
            $parent = $t.parent();
            name = $parent.attr('action-target');
            cid = '.js_screen_'+name;
            val = $t.attr('action-data');
            $(cid).val(val);
            if(name == 'city'){//如果点击城市
                $('.js_screen_univs').val(0);
            }
            $activeScreenSub.click();
        });
        seajs.use('dist/icheck.min.js',function(){
            $checkboxScreen.off('ifChecked');
            $checkboxScreen.off('ifUnchecked');
            $checkboxScreen.on({
                'ifChecked':function(){
                    var $t = $(this);
                    $t.val(1);
                    $activeScreenSub.click();
                },
                'ifUnchecked':function(){
                    var $t = $(this);
                    $t.val(0);
                    $activeScreenSub.click();
                }
            });
            $activityMySchool.off('ifUnchecked').on('ifUnchecked',function(){
                var $t = $(this);
                $t.val(0);
                $('.js_screen_city,.js_screen_univs').val(0);
                $activeScreenSub.click();
            });
        });
    };
    $(function(){
        popToggle.init();
        popMaskClose();
        selectSchoolAct();//执行选择学校方法
        if($skSlider){//判断是否使用轮播功能
            seajs.use('dist/sk.infinity.slider-2.min.js',function(){
                $skSlider.slider2({
                    auto:true,
                    screenwidth:true,
                    time:5000
                });//执行轮播
            });
        }
        moreRankAct();
        moreRankDisplay();
        seajs.use('dist/sk.set.datetime.min.js',function(){
            function validateTime(){//校验开始时间、结束时间
                var $jsEndTime = $('input.js_endTime'),
                    $jsStartTime = $('input.js_startTime'),
                    startTime,
                    strStartTime,
                    endTime,
                    strEndTime,
                    date = new Date();
                $jsEndTime.datepicker({
                    onClose : function(){
                        var $t = $(this),
                            $selfStartTime = $t.parents('.js_datetimepicker_box').find('input.js_startTime');
                        strStartTime = $selfStartTime.val();
                        len = new String(strStartTime).length;
                        strEndTime = $t.val();
                        startTime = Date.parse(strStartTime);
                        endTime = Date.parse(strEndTime);
                        if(len > 0 && (startTime >= endTime)){
                            ajax_message('结束时间不能早于开始时间',true);
                            setTimeout(function(){
                                $ajaxMessageBox.fadeOut();
                            },1000);
                            $t.val('');
                            return false;
                        }
                    }
                });
                $jsStartTime.datepicker({
                    onClose : function(){
                        var $t = $(this),
                            $selfEndTime = $t.parents('.js_datetimepicker_box').find('input.js_endTime');
                        strStartTime = $t.val();
                        strEndTime = $selfEndTime.val();
                        len = new String(strEndTime).length;
                        startTime = Date.parse(strStartTime);
                        endTime = Date.parse(strEndTime);
                        if(len > 0 && (startTime >= endTime)){
                            ajax_message('结束时间不能早于开始时间');
                            $t.val('');
                            return false;
                        }
                    }
                });
            };
            dateTimePicker(validateTime);
        });
        timeScreenAct();
        activeScreenAct();//执行活动筛选方法
        dropDownFun.init();
        dropDownFun.casDropDown();//执行级联下拉菜单方法
    });
})();
